#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

namespace my {

    typedef long long ll;
    const int maxn=2000, maxq=200000, maxx=1000000000, sqrtx=31623;
    ll powa[2][sqrtx+1], powb[2][sqrtx+1];
    int n, q, a, b, w, h, mods, mapx[maxn+2], mapy[maxn+2], sum[4][maxn+2][maxn+2], xn, yn;

    struct Point {
        int x, y, h;

        static bool cmpX(Point &a, Point &b) {return a.x<b.x;}
        static bool cmpY(Point &a, Point &b) {return a.y<b.y;}

    } sate[maxn+1];

    void initPow(int n, int a, int b) {
        powa[0][0] = powa[1][0] = powb[0][0] = powb[1][0] = 1;
        for (int i=1; i<=sqrtx; i++) {
            powa[0][i] = powa[0][i-1]*a%mods;
            powb[0][i] = powb[0][i-1]*b%mods;
        }
        for (int i=1; i*sqrtx<=n; i++) {
            powa[1][i] = powa[1][i-1]*powa[0][sqrtx]%mods;
            powb[1][i] = powb[1][i-1]*powb[0][sqrtx]%mods;
        }
    }

    ll apow(int n) {return n>=0 ? powa[0][n%sqrtx]*powa[1][n/sqrtx]%mods : 0;}

    ll bpow(int n) {return n>=0 ? powb[0][n%sqrtx]*powb[1][n/sqrtx]%mods : 0;}

    ll inc(ll &a, ll b) {
        return a=(a+b)%mods;
    }

    void reNum() {
        sort(sate+1, sate+n+1, Point::cmpX);
        for (int i=1; i<=n; i++) {
            if (sate[i].x!=mapx[xn]) {
                mapx[++xn] = sate[i].x;
            }
            sate[i].x = xn;
        }

        sort(sate+1, sate+n+1, Point::cmpY);
        for (int i=1; i<=n; i++) {
            if (sate[i].y!=mapy[yn]) {
                mapy[++yn] = sate[i].y;
            }
            sate[i].y = yn;
        }
    }

    void buildSum() {
        static ll h[maxn+1][maxn+1];
        for (int i=1; i<=n; i++) {
            inc(h[sate[i].x][sate[i].y], sate[i].h);
        }
        mapx[xn+1] = mapx[xn], mapy[yn+1] = mapy[yn];

        for (int i=1; i<=xn; i++) {
            ll t=0;
            for (int j=1; j<=yn; j++) {
                t = (t*bpow(mapy[j]-mapy[j-1])%mods+h[i][j])%mods;
                sum[0][i][j] = (sum[0][i-1][j]*apow(mapx[i]-mapx[i-1])%mods+t)%mods;
            }

            t = 0;
            for (int j=yn; j>0; j--) {
                t = (t*bpow(mapy[j+1]-mapy[j])%mods+h[i][j])%mods;
                sum[1][i][j] = (sum[1][i-1][j]*apow(mapx[i]-mapx[i-1])%mods+t)%mods;
            }
        }

        for (int i=xn; i>0; i--) {
            ll t=0;
            for (int j=1; j<=yn; j++) {
                t = (t*bpow(mapy[j]-mapy[j-1])%mods+h[i][j])%mods;
                sum[2][i][j] = (sum[2][i+1][j]*apow(mapx[i+1]-mapx[i])%mods+t)%mods;
            }
            t = 0;
            for (int j=yn; j>0; j--) {
                t = (t*bpow(mapy[j+1]-mapy[j])%mods+h[i][j])%mods;
                sum[3][i][j] = (sum[3][i+1][j]*apow(mapx[i+1]-mapx[i])%mods+t)%mods;
            }
        }
    }

    int findBelow(int n, int a[], int t) {
        int ret=0;
        for (int l=1, r=n, mid; l<=r;) {
            mid=(l+r)/2;
            if (a[mid]<=t) {
                ret = mid;
                l = mid+1;
            } else r = mid-1;
        }
        return ret;
    }

    ll getAns(int p, int q) {
        int tx=findBelow(xn, mapx, p), ty=findBelow(yn, mapy, q);
        ll e=apow(p-mapx[tx]), f=bpow(q-mapy[ty]), g=apow(mapx[tx+1]-p), h=bpow(mapy[ty+1]-q);
        return (sum[0][tx][ty]*e%mods*f%mods+
                sum[1][tx][ty+1]*e%mods*h%mods+
                sum[2][tx+1][ty]*g%mods*f%mods+
                sum[3][tx+1][ty+1]*g%mods*h%mods)%mods;
    }

    int main() {
        freopen("satellite.in", "r", stdin);
        freopen("satellite.out", "w", stdout);

        scanf("%d %d %d %d %d %d %d", &n, &q, &w, &h, &mods, &a, &b);
        initPow(maxx, a, b);
        for (int i=1; i<=n; i++) {
            scanf("%d %d %d", &sate[i].h, &sate[i].x, &sate[i].y);
        }
        reNum();
        buildSum();
        for (int i=1; i<=q; i++) {
            int x, y;
            scanf("%d %d", &x, &y);
            printf("%lld\n", getAns(x, y));
        }

        fclose(stdin);
        fclose(stdout);
        return 0;
    }
};

int main() {return my::main();}
